﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
               "Administration", // Route name
               "Admin", // URL with parameters
               new { controller = "Admin", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
               "Poslovni_prostori", // Route name
               "poslovni_prostori", // URL with parameters
               new { controller = "poslovni_prostori", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
               "OnlyActionName", // Route name
               "{action}", // URL with parameters
               new { controller = "Home", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Search", // Route name
                "{controller}/{action}/{id}/{roomsNumberId}/{areaId}/{priceId}", // URL with parameters
                new { controller = "Xml", action = "Search", id = UrlParameter.Optional, roomsNumberId = UrlParameter.Optional, areaId = UrlParameter.Optional, priceId = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            var currentUrl = HttpContext.Current.Request.Url.ToString().ToLower();
            if (currentUrl.StartsWith("http://centar-bundek"))
            {
                var newUrl = currentUrl.Replace("http://centar-bundek", "http://www.centar-bundek");
                Response.RedirectPermanent(newUrl);
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            //CheckDeviceType();
        }

        private void CheckDeviceType()
        {
            // Redirect mobile users to the mobile home page
            var httpRequest = HttpContext.Current.Request;
            if (httpRequest.Browser.IsMobileDevice)
            {
                HttpContext.Current.Response.Redirect("http://m.centar-bundek.hr");
            }
        }
    }
}